package com.joywifi.android.demo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.joywifi.android.demo.R;
import com.joywifi.android.demo.fragment.CustomeWebViewFragment;
import com.joywifi.android.demo.fragment.MainButtomTabFragment;
import com.joywifi.android.demo.fragment.MainRecycleFragment;
import com.joywifi.android.demo.fragment.MainViewPagerFragment;
import com.joywifi.android.demo.utils.UiUtils;
import com.joywifi.android.demo.widget.BackHandledFragment;

import butterknife.ButterKnife;


public class MainActivity extends AppCompatActivity implements BackHandledFragment.BackHandlerInterface {

    private Toolbar mToolbar;
    private DrawerLayout mDrawerLayout;
    private BackHandledFragment selectedFragment;
    private NavigationView mNavigationView;
    private ActionBarDrawerToggle mDrawerToggle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ButterKnife.bind(this);

        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(mToolbar);

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar, R.string.drawer_open, R.string.drawer_close);
        mDrawerToggle.syncState();
        mDrawerLayout.setDrawerListener(mDrawerToggle);

        mNavigationView = (NavigationView) findViewById(R.id.navigation);
        setupDrawerContent(mNavigationView);

        switchViewPager();
    }

    private void switchViewPager() {
        getSupportFragmentManager().beginTransaction().replace(R.id.main_frame, new MainViewPagerFragment()).commit();
    }

    private void switchRecycleView() {
        getSupportFragmentManager().beginTransaction().replace(R.id.main_frame, new MainRecycleFragment()).commit();
    }

    private void switchButtomTabView() {
        getSupportFragmentManager().beginTransaction().replace(R.id.main_frame, new MainButtomTabFragment()).commit();
    }

    private void switchWebView() {
        getSupportFragmentManager().beginTransaction().replace(R.id.main_frame, new CustomeWebViewFragment()).commit();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_settings:
                UiUtils.snackbar(getCurrentFocus(), "设置");
                return true;
            case R.id.action_search:
                UiUtils.snackbar(getCurrentFocus(), "搜索");
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    private void setupDrawerContent(NavigationView navigationView) {
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem) {
                switch (menuItem.getItemId()) {
                    case R.id.menu_drawer_recycle:
                        switchRecycleView();
                        break;
                    case R.id.menu_drawer_bottom_tab:
                        switchButtomTabView();
                        break;
                    case R.id.menu_drawer_view_pager:
                        switchViewPager();
                        break;
                    case R.id.menu_drawer_web_view:
                        switchWebView();
                        break;
                    case R.id.menu_drawer_setting:
                        Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
                        startActivity(intent);
                    default:
                }
                menuItem.setChecked(true);
                mDrawerLayout.closeDrawers();
                return true;
            }
        });
    }

    @Override
    public void setSelectedFragment(BackHandledFragment backHandledFragment) {
        this.selectedFragment = backHandledFragment;
    }

    private long exitTime = 0;

    private void doExitApp() {
        if ((System.currentTimeMillis() - exitTime) > 2000) {
            Toast.makeText(this, R.string.press_again_exit_app, Toast.LENGTH_SHORT).show();
            exitTime = System.currentTimeMillis();
        } else {
            finish();
        }
    }

    @Override
    public void onBackPressed() {
        if (selectedFragment == null || !selectedFragment.onBackPressed()) {
            if (mDrawerLayout.isDrawerOpen(Gravity.LEFT)) {
                mDrawerLayout.closeDrawer(Gravity.LEFT);
            } else {
                doExitApp();
            }
        }

    }
}
